package com.ggboy.sc.pl.project.es.util;

import org.aspectj.lang.annotation.Before;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Component;

import com.ggboy.sc.pl.project.es.model.SampleEntity;

import javax.annotation.Resource;
import java.net.UnknownHostException;

@Component
public class ElasticsearchTemplateUtil {
    private static final String INDEX_NAME = "test-index";
    private static final String INDEX_1_NAME = "test-index-1";
    private static final String INDEX_2_NAME = "test-index-2";
    private static final String TYPE_NAME = "test-type";
    
    @Resource(name = "elasticsearchTemplate")
    private ElasticsearchTemplate elasticsearchTemplate;
//    @Resource
//    private PostRepository postRepository;
    public void before() {
        elasticsearchTemplate.deleteIndex(SampleEntity.class);
        elasticsearchTemplate.createIndex(SampleEntity.class);
        elasticsearchTemplate.deleteIndex(INDEX_1_NAME);
        elasticsearchTemplate.deleteIndex(INDEX_2_NAME);
//        elasticsearchTemplate.deleteIndex(UseServerConfigurationEntity.class);
        elasticsearchTemplate.refresh(SampleEntity.class);
    }
    public void recreateIndex() throws UnknownHostException {
        elasticsearchTemplate.typeExists(INDEX_NAME,TYPE_NAME);
//        elasticsearchTemplate.index()
//        if (client.admin().indices().prepareExists(indexName).execute().actionGet()
//                .isExists()) {
//            DeleteIndexResponse deleteIndexResponse = client.admin().indices()
//                    .delete(new DeleteIndexRequest(indexName)).actionGet();
//            System.out.println("delete index :" + deleteIndexResponse);
//        }
//
//        CreateIndexResponse createIndexResponse = client.admin().indices()
//                .prepareCreate(indexName).execute().actionGet();
//        System.out.println("create index :" + createIndexResponse);
    }


    public ElasticsearchTemplate getElasticsearchTemplate() {
        return elasticsearchTemplate;
    }

    public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
        this.elasticsearchTemplate = elasticsearchTemplate;
    }
}
